package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Set;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import viewInterfaces.IViewTestResultGraphic;
import controllerInterfaces.IControllerTestResultGraphic;

/**
 * View used to display the graph generated from the test results, including a chart symbology for each algorithm.
 * 
 * @author Andy Cullache.
 */
public class ViewTestResultGraphic extends JFrame implements IViewTestResultGraphic{

	private IControllerTestResultGraphic myController;
	private String metric;
	private JPanel contentPane;
	private JPanel panelGraphic;
	private JPanel panelSimbology;
	private Color[] colors = {Color.BLUE,Color.RED,Color.MAGENTA,Color.ORANGE,Color.DARK_GRAY
			,Color.GREEN,Color.LIGHT_GRAY,Color.GRAY,Color.PINK,Color.YELLOW,Color.CYAN};

	/**
	 * Default constructor.
	 */
	public ViewTestResultGraphic() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 531, 389);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		panelGraphic = new JPanel();
		
		panelSimbology = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(panelGraphic, GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
						.addComponent(panelSimbology, GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE))
					.addGap(3))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addComponent(panelGraphic, GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panelSimbology, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
		);
		panelSimbology.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		contentPane.setLayout(gl_contentPane);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
	}

	/**
	 * Sets the controller of this view.
	 * 
	 * @param myController      Test result graphic controller.
	 */
	public void SetController(IControllerTestResultGraphic controllerTestGraphic) {
		this.myController = controllerTestGraphic;
	}

	/**
	 * Add the graph to the main panel of this view.
	 * 
	 * @param      Graphic component.
	 */
	public void setGraphic(Component graphic) {
        panelGraphic.add(graphic);
        graphic.setVisible(true);
        graphic.setBounds(10,15,480,275);
        this.setLocationRelativeTo(null);
	}

	/**
	 * Add a symbolism for each algorithm graph, one color for each algorithm.
	 * 
	 * @param keyAlgorithms      List of algorithms to graph.
	 */
	public void setAlgorithms(Set<String> keyAlgorithms) {
		JLabel lblNewLabel;
		int color= 0;
		for(String algorithm : keyAlgorithms){
			lblNewLabel = new JLabel(algorithm);
			lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
			lblNewLabel.setForeground(this.colors[color]);
			panelSimbology.add(lblNewLabel);
			color++;
		}
	}

	/**
	 * Set the metric occupied in the graphics algorithms.
	 * 
	 * @param metric      Metric that evaluates algorithms.
	 */
	public void setMetric(String metric) {
		panelGraphic.setBorder(new TitledBorder(null, "Metric: "+metric, TitledBorder.LEADING, TitledBorder.TOP, null, null));
	}
}
